package com.briup.facade.web.controller;

import com.briup.common.util.PageResult;
import com.briup.common.web.Message;
import com.briup.common.web.MessageUtil;
import com.briup.logging.annotation.LoggingAccess;
import com.briup.model.entity.Model;
import com.briup.model.service.IModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@LoggingAccess(value = "模型管理")
@RestController
@Api(tags = "模型管理模块")
public class ModelController {
    @Autowired
    private IModelService modelService;

    // 创建模型
    @LoggingAccess(value = "创建模型")
    @ApiOperation(value = "创建模型", notes = "参数类型为json,model中提供了注释")
    @PostMapping("/model/")
    public Message insertModel(@RequestBody Model model) {
        modelService.insertModel(model);
        return MessageUtil.success("创建成功");
    }

    // 查看模型信息
    @LoggingAccess(value = "查看模型")
    @ApiOperation(value = "查看模型", notes = "只需要返回模型id和模型名称")
    @GetMapping("/model/")
    public Message findModel() {
        // 前端只需要模型id和模型名称
        List<Model> modelList = modelService.findModel();
        return MessageUtil.success(modelList);
    }


    // 根据模型id和模型训练版本查看模型训练结果和评估结果等信息
    @LoggingAccess(value = "查看模型训练和模型评估相关信息")
    @ApiOperation(value = "查看模型训练和模型评估相关信息", notes = "训练模块提供了版本选择")
    @GetMapping("/model/{modelId}/{modelVersion}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "模型id", paramType = "path", dataType = "int", required = true),
            @ApiImplicitParam(name = "modelVersion",value = "训练版本",paramType = "path",dataType = "String",required = true)
    })
    public Message findModelExByIdAndVersion(@PathVariable("modelId") Integer modelId, @PathVariable("modelVersion") String modelVersion) {
        Map<String, Object> traingExAndEvaluateEx = modelService.findModelExByIdAndVersion(modelId, modelVersion);
        return MessageUtil.success(traingExAndEvaluateEx);
    }

    // 根据模型id删除模型信息(逻辑删除)
    @LoggingAccess("删除模型")
    @ApiOperation(value = "删除模型",notes = "逻辑删除")
    @DeleteMapping("/model/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模型id", paramType = "path", dataType = "int", required = true)
    })
    public Message deleteModel(@PathVariable("id")Integer id){
        modelService.deleteModel(id);
        return MessageUtil.success("删除成功");
    }

    // 根据模型id查看详情(模型,训练拓展,评估拓展)
    // 此处只拓展该模型下最新版本的训练以及评估
    @LoggingAccess("查看详情")
    @ApiOperation(value = "查看详情",notes = "返回值拓展了最新版本的训练和评估情况")
    @GetMapping("/model/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模型id", paramType = "path", dataType = "int", required = true)
    })
    public Message modelInfo(@PathVariable("id") Integer id){
        Map<String,Object> modelWithTrainExAndEvaluateEx = modelService.modelInfo(id);
        return MessageUtil.success(modelWithTrainExAndEvaluateEx);
    }

    // 根据模型部位id和状态id分页查看模型列表
    @LoggingAccess("条件并分页查看模型列表")
    @ApiOperation(value = "条件并分页查看模型列表",notes = "此处条件为模型状态和模型类型)")
    @PostMapping("/model/{pageNum}/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", paramType = "path", dataType = "int", required = true,defaultValue = "0"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "path", dataType = "int", required = true,defaultValue = "5")
    })
    public Message findModelByCondition(@RequestBody Model model,
                                        @PathVariable("pageNum") Integer pageNum,
                                        @PathVariable("pageSize") Integer pageSize){
        PageResult<Model> pageResult = modelService.findModelByCondition(model,pageNum,pageSize);

        return MessageUtil.success(pageResult);
    }
}
